
import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { CirclePieEcharts } from '@app/shared/ehcartsAbout/circlePieEcharts';

@Component({
  selector: 'app-home-top-circle-pie-echart',
  templateUrl: './home-top-circle-pie-echart.component.html',
  styleUrls: ['./home-top-circle-pie-echart.component.less']
})
export class HomeTopCirclePieEchartComponent implements OnInit, OnChanges {
  @Input() mergeData: any;
  @Input() legendValueList: any; //legendvalue 的值

  public options: any;  //基础结构
  public merge: any;    //将要合并的数据
  public echartOjb: CirclePieEcharts;

  constructor() { }

  ngOnInit() {

  }
  ngOnChanges(changes: SimpleChanges): void {
    if (changes.hasOwnProperty('mergeData')) {
      if (changes.legendValueList && changes.legendValueList.currentValue.length > 0) {
        this.echartOjb = new CirclePieEcharts(this.legendValueList);
        this.options = this.echartOjb.option;
      }
      if (this.mergeData !== undefined) {
        //初始化图表
        this.initChart()
        //更新图表数据
        this.update();
      }
    }
  }
  // 初始化图表数据
  initChart() {
    const originalOptions = this.merge === undefined
      ? JSON.parse(JSON.stringify(this.echartOjb.option))
      : JSON.parse(JSON.stringify(this.merge));
    this.echartOjb.option = originalOptions;
    this.merge = JSON.parse(JSON.stringify(this.echartOjb.option));
  }
  // 更新图表数据
  update() {
    const merge = this.merge === undefined
      ? JSON.parse(JSON.stringify(this.echartOjb.option))
      : JSON.parse(JSON.stringify(this.merge));
    merge.graphic[0].style.text = this.mergeData.graphic0StyleText;
    merge.graphic[0].left = this.mergeData.graphic0Left;
    merge.series[0].data = this.mergeData.seriesData;
    this.merge = JSON.parse(JSON.stringify(merge));
  }
}
